package com.bawei.module_motocircle.ui.post

import com.bawei.lib_net.BaseResponse
import com.bawei.module_motocircle.http.MotorCircleApi
import com.bawei.module_motocircle.http.MotorCircleNetModule
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import retrofit2.http.GET
import retrofit2.http.Query
import javax.inject.Inject

/**
 * @author yaotianxue
 * @description
 * @date 2025/6/2
 */
class PostListRepository @Inject constructor() {
    @Inject
    @MotorCircleNetModule.CommonMainApi
    lateinit var motorCircleApi: MotorCircleApi

    /**
     *  检查是否关注
     */
    fun checkIfHasFocus() = flow {
        emit(motorCircleApi.checkIfHasFocus())
    }.flowOn(Dispatchers.IO)

    /**
     * 帖子列表
     */
    fun pageList(
        sortBy: String? = null,
        topicId: String? = null,
        memberId: String? = null,
        ifFocus: Int? = null,
        pageNum: Int,
        pageSize: Int = 10,
        keyword: String? = null
    ) = flow {
        emit(
            motorCircleApi.pageList(
                sortBy,
                topicId,
                memberId,
                ifFocus,
                pageNum,
                pageSize,
                keyword
            )
        )
    }.flowOn(Dispatchers.IO)


    /**
     * 点赞帖子
     */
    fun giveLike(postsId: String) = flow {
       emit(motorCircleApi.giveValidationLike(postsId)) //校验帖子
    }.map {
        if (it.data){//校验通过
            motorCircleApi.giveLike(postsId) //点赞和取消点赞
        }else{//校验不通过
            it
        }
    }.flowOn(Dispatchers.IO)
}